﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Imaging;
using System.Xml;
using DB;

namespace API
{
    public class ProductImageApi
    {
        private static string strLocalCache = @"C:\Images\Products\";
        private static Dictionary<string, List<Image>> dicImage = new Dictionary<string,  List<Image>>();
        public static List<Image> GetImages(Product product)
        {
            var listRtn = GetImages(product.SKU);
            return listRtn;
        }
        public static List<Image> GetImages(string sku)
        {
            if (!dicImage.ContainsKey(sku))
            {
                var temp = LoadLocalImages(sku);
                dicImage.Add(sku, temp);
            }
            return dicImage[sku];

        }

        private static List<Image> GetImageFromStore(string sku)
        {
            var imagesRtn = new List<Image>();
            var urls = StoreApi.Api.GetProductImageUrls(sku);
            int i = 1;
            foreach (var url in urls)
            {
                var image = GetImagebyUrl(url, strLocalCache + sku + "_PIC" + i + ".jpg");
                i++;
                imagesRtn.Add(image);
            }
            return imagesRtn;
        }
        private static List<Image> GetImageFromMailServer(string sku)
        {
            var imagesRtn = new List<Image>();
            for (int i = 1; i < 4;i++ )
            {
                var image = GetImagebyUrl("http://mail.maibey.com/Images/Products/" + sku + "_PIC" + i + ".jpg", strLocalCache + sku + "_PIC" + i + ".jpg");
                if (image != null)
                    imagesRtn.Add(image);
            }
            return imagesRtn;
        }
        public static Image GetImagebyPath(string imagePath)
        {
            try
            {
                return Image.FromFile(imagePath);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError(ex.ToString());
                return null;
            }
        }
        public static Image GetImagebyUrl(string fileOnlinePath,string imagePath)
        {
            System.Drawing.Image image = null;
            try
            {
                if (!Directory.Exists(strLocalCache))
                    Directory.CreateDirectory(strLocalCache);
                using (var wc = new WebClient())
                {
                    wc.DownloadFile(fileOnlinePath, imagePath);
                }
                var fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
                image = System.Drawing.Image.FromStream(fs);
                fs.Close();
            }
            catch (Exception exception)
            {
            }
            return image;
        }
        private static List<Image> LoadLocalImages(string sku)
        {
            var imagesRtn = new List<Image>();
            for (int i = 1; i <= 4; i++)
            {
                if (File.Exists(strLocalCache + sku + "_PIC" + i + ".jpg"))
                {
                    var image = GetImagebyPath(strLocalCache + sku + "_PIC" + i + ".jpg");
                    if(image != null)
                        imagesRtn.Add(image);
                }
            }
            if (imagesRtn.Count == 0)
            {
                imagesRtn = GetImageFromMailServer(sku);
            }
            return imagesRtn;
        }
        
        private static WebClient _client;
        public static bool IsURLFileExists(string url)
        {
            Stream stream = null;
            try
            {
                if (_client == null)
                    _client = new WebClient();
                stream = _client.OpenRead(url);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }
        }
        #region IDisposable Members

        public void Dispose()
        {
            if (_client != null)
                _client.Dispose();
        }

        #endregion
    }
}
